<?php

/*
 * Helper functions
*/

// get the js for media uploader

function bierden_media_uploader_enqueue_scripts() {
	wp_register_script( 'bierden_media_upload', get_template_directory_uri() .'/js/media_uploader.js', array('jquery', 'media-upload') );
	wp_enqueue_script('jquery');
	if ( 'appearance_page_bierden_theme_options' == get_current_screen() -> id ) {
		wp_enqueue_script('jquery');
		wp_enqueue_script('media-upload');
		wp_enqueue_script('bierden_media_upload');

	}

}
add_action('admin_enqueue_scripts', 'bierden_media_uploader_enqueue_scripts');

function bierden_options_setup() {
	wp_enqueue_script('jquery');
	wp_enqueue_media();
}
add_action( 'admin_init', 'bierden_options_setup' );


/*
 * actual options api
 */

// Add "bierden Options" link to the "Appearance" menu
function bierden_menu_options() {
	add_theme_page('bierden Options', __( 'Bierden Options', 'tsv-bierden'), 'edit_theme_options', 'bierden_theme_options', 'bierden_admin_options_page');
}
// Load the Admin Options page
add_action('admin_menu', 'bierden_menu_options');



function bierden_admin_options_page() { ?>
	<div class="wrap">
		<h2><?php _e('My Theme Options' , 'tsv-bierden' ); ?></h2>
		<?php settings_errors();
		
			if( isset( $_GET[ 'tab' ] ) ) {
				$active_tab = $_GET[ 'tab' ];
				if( $active_tab == 'general_options' ) {
					$active_tab = 'general_options';
				} else if( $active_tab == 'social_options' ) {
					$active_tab = 'social_options';
				} else if( $active_tab == 'media_options' ) {
					$active_tab = 'media_options';
				} else {
					$active_tab = 'general_options';
				} // end if/else  
			} else {
				$active_tab = 'general_options';
			} // end if/else 
		?>

		<h2 class="nav-tab-wrapper">
			<a href="?page=bierden_theme_options&tab=general_options" class="nav-tab <?php echo $active_tab == 'general_options' ? 'nav-tab-active' : ''; ?>"><?php _e( 'General Options', 'tsv-bierden' ); ?></a>
			<a href="?page=bierden_theme_options&tab=social_options" class="nav-tab <?php echo $active_tab == 'social_options' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Social Options', 'tsv-bierden' ); ?></a>
			<a href="?page=bierden_theme_options&tab=media_options" class="nav-tab <?php echo $active_tab == 'media_options' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Media Options', 'tsv-bierden' ); ?></a>
		</h2>
		
		<form method="post" action="options.php">
			<?php
			
				if( $active_tab == 'general_options' ) {

					settings_fields( 'bierden_theme_general_options' );
					do_settings_sections( 'bierden_theme_general_options' );
				
				} else if( $active_tab == 'social_options' ) {
				
					settings_fields( 'bierden_theme_social_options' );
					do_settings_sections( 'bierden_theme_social_options' );
					
				} elseif( $active_tab == 'media_options' ) {

					settings_fields( 'bierden_theme_media_options' );
					do_settings_sections( 'bierden_theme_media_options' );
					
				}  // end if/else
				
				submit_button();
			
			?>
		</form>
    </div>
<?php
}


/* ------------------------------------------------------------------------ *
 * Setting Registration
* ------------------------------------------------------------------------ */

/**
 * Provides default values for the GenrealOptions
 */
function bierden_theme_default_general_options() {

	$defaults = array (
		'home_page_categorie' => '',
	);

	return apply_filters( 'bierden_theme_default_general_options', $defaults );

} // end bierden_theme_default_general_options


/**
 * Provides default values for the Social Options
 */
function bierden_theme_default_social_options() {

  $defaults = array (
      'fussball_de' =>  '',
      'twitter'		=>	'',
      'facebook'    =>	'',
      'googleplus'	=>	'',
  );

  return apply_filters( 'bierden_theme_default_social_options', $defaults );

} // end bierden_theme_default_social_options

/**
 * Provides default values for the Media Options 
 */
function bierden_theme_default_media_options() {
	
	$defaults = array (
		'header_image' => '',
	);
	
	return apply_filters( 'bierden_theme_default_media_options', $defaults);
	
}  // end bierden_theme_default_media_options

function bierden_theme_intialize_general_options() {

	// If the theme options don't exist, create them.
	if( false == get_option( 'bierden_theme_general_options' ) ) {
		add_option( 'bierden_theme_general_options', apply_filters( 'bierden_theme_default_general_options', bierden_theme_default_general_options() ) );
	}
	
	// Settings section
	add_settings_section( 'general_settings_section', __( 'General Options', 'tsv-bierden' ), 'bierden_general_settings_section_text', 'bierden_theme_general_options' );
	// Settings fields
	add_settings_field( 'home_page_categorie_input', __( 'Categories', 'tsv-bierden' ), 'bierden_home_page_categorie_input', 'bierden_theme_general_options', 'general_settings_section' );
	
	// Register settings
	register_setting( 'bierden_theme_general_options', 'bierden_theme_general_options', 'bierden_theme_genereal_options_validate');
	
}
add_action( 'admin_init', 'bierden_theme_intialize_general_options' );

function bierden_theme_intialize_social_options() {

    // If the theme options don't exist, create them.
    if( false == get_option( 'bierden_theme_social_options' ) ) {
      add_option( 'bierden_theme_social_options', apply_filters( 'bierden_theme_default_social_options', bierden_theme_default_social_options() ) );
    }

	// Settings section
	add_settings_section( 'social_settings_section', __( 'Social Options', 'tsv-bierden' ), 'bierden_social_settings_section_text', 'bierden_theme_social_options' );
	// Settings fields
	add_settings_field( 'fussball_de_input', 'Fussball.de Account', 'bierden_fussball_de_input', 'bierden_theme_social_options', 'social_settings_section' );
	add_settings_field( 'twitter_input', 'Twitter Account', 'bierden_twitter_input', 'bierden_theme_social_options', 'social_settings_section' );
	add_settings_field( 'facebook_input', 'Facebook Account', 'bierden_facebook_input', 'bierden_theme_social_options', 'social_settings_section' );	
	add_settings_field( 'google_plus_input', 'Google+ Account', 'bierden_google_plus_input', 'bierden_theme_social_options', 'social_settings_section' );
	
	// Register settings
	register_setting( 'bierden_theme_social_options', 'bierden_theme_social_options', 'bierden_theme_social_options_validate' );
}
add_action( 'admin_init', 'bierden_theme_intialize_social_options');

function bierden_theme_intialize_media_options() {
	// If the theme options don't exist, create them.
	if( false == get_option( 'bierden_theme_media_options' ) ) {
		add_option( 'bierden_theme_media_options', apply_filters( 'bierden_theme_default_media_options', bierden_theme_default_media_options() ) );
	}
	
	// Settings section
	add_settings_section( 'media_settings_section', __( 'Media Options', 'tsv-bierden' ), 'bierden_media_settings_section_text', 'bierden_theme_media_options' );
	// Settings fields
	add_settings_field( 'header_image_upload', __( 'Header Image Upload', 'tsv-bierden' ), 'bierden_header_image_upload', 'bierden_theme_media_options', 'media_settings_section' );
	
	// Register settings
	register_setting( 'bierden_theme_media_options', 'bierden_theme_media_options', 'bierden_theme_media_options_validate');
}
add_action( 'admin_init', 'bierden_theme_intialize_media_options' );



/* ------------------------------------------------------------------------ *
 * Section Callbacks
 * ------------------------------------------------------------------------ */

/**
 * This function provides a simple description for the General Options page.
 *
 * It's called from the 'sandbox_initialize_theme_options' function by being passed as a parameter
 * in the add_settings_section function.
 */

function bierden_general_settings_section_text() {
	echo '<p>' . __( 'Select the Categorie which should be displayed at the Home Page.', 'tsv-bierden' ) . '</p>';
}

function bierden_social_settings_section_text() {
    echo '<p>' . __( 'Provide the URL to the social networks you\'d like to display.', 'tsv-bierden' ) . '</p>';
}

function bierden_media_settings_section_text() {
	echo '<p>' . __( 'Upload the Images you\'d like to display.', 'tsv-bierden' ) . '</p>';
}


/* ------------------------------------------------------------------------ *
 * Field Callbacks
 * ------------------------------------------------------------------------ */

/**
 * This function renders the interface elements for toggling the visibility of the header element.
 *
 * It accepts an array or arguments and expects the first element in the array to be the description
 * to be displayed next to the checkbox.
 */

function bierden_home_page_categorie_input() {
	// First, we read the general options collection
	$options = get_option( 'bierden_theme_general_options' );
	$cat = '1';
	if( isset( $options['home_page_categorie'] ) ) {
		$cat = $options['home_page_categorie'];
	} // end if
	$categories = get_categories( 'hide_empty=0' );
	echo '<select id="categorie_select" name="bierden_theme_general_options[home_page_categorie]">';
	foreach ($categories as $cats) {
		echo '<option value="' . $cats->cat_ID .'"' . selected( $cats->cat_ID, $cat ) . '>' . $cats->cat_name . '</option>';
	}
	echo '</select>';
}

function bierden_fussball_de_input() {

	// First, we read the social options collection
	$options = get_option( 'bierden_theme_social_options' );
	
	// Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.
	$url = '';
	if( isset( $options['fussball_de'] ) ) {
	  $url = esc_url( $options['fussball_de'] );
	} // end if
	// Render the output
	echo '<input type="text" id="fussball_de" name="bierden_theme_social_options[fussball_de]" value="' . $url . '" />';

} // end sandbox_twitter_callback

function bierden_twitter_input() {

	// First, we read the social options collection
	$options = get_option( 'bierden_theme_social_options' );
	
	// Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.
	$url = '';
	if( isset( $options['twitter'] ) ) {
	  $url = esc_url( $options['twitter'] );
	} // end if
	
	// Render the output
	echo '<input type="text" id="twitter" name="bierden_theme_social_options[twitter]" value="' . $url . '" />';

} // end sandbox_twitter_callback

function bierden_facebook_input() {

	$options = get_option( 'bierden_theme_social_options' );
	
	$url = '';
	if( isset( $options['facebook'] ) ) {
	  $url = esc_url( $options['facebook'] );
	} // end if
	
	// Render the output
	echo '<input type="text" id="facebook" name="bierden_theme_social_options[facebook]" value="' . $url . '" />';

} // end sandbox_facebook_callback

function bierden_google_plus_input() {

	$options = get_option( 'bierden_theme_social_options' );
	
	$url = '';
	if( isset( $options['googleplus'] ) ) {
	  $url = esc_url( $options['googleplus'] );
	} // end if
	
	// Render the output
	echo '<input type="text" id="googleplus" name="bierden_theme_social_options[googleplus]" value="' . $url . '" />';

} // end sandbox_googleplus_callback

function bierden_header_image_upload() { 
	$options = get_option( 'bierden_theme_media_options' );
	$url = 'http://';
	if ( isset( $options['header_image'] ) ) {
		$url = esc_url( $options['header_image'] );
	}
	
	// Render the output
	//@Todo img tag should not be displayed if not value is there
	echo '<input id="upload_header_image_url" type="hidden" size="36" name="bierden_theme_media_options[header_image]" value="' . $url . '" />';
	echo '<img class="upload_image_preview" src="' . $url . '">';
	echo '<button id="header" class="upload_image_button button" type="button">' . __( 'Upload Image' , 'tsv-bierden' ) . '</button>';
}

function bierden_theme_genereal_options_validate($input) {
	$valid_options = get_option( 'bierden_theme_general_options' );
	$valid_options_keys = array_keys($valid_options);
	if (is_null(term_exists((int) $input[$valid_options_keys[0]], 'category'))) {
		// Settings error
		add_settings_error( 'category_error', esc_attr( 'unknown_category' ), __( 'An unknown Category had been selected.', 'tsv-bierden' ) );
		return	$valid_options;
	}
	
	return $input;
}

function bierden_theme_social_options_validate($input) {
	$valid_options = get_option( 'bierden_theme_social_options' );
	$valid_proof = true;
	$error_counter = 0;
	foreach ($input as $social_key => $social_value) {
		if (!filter_var($social_value, FILTER_VALIDATE_URL)  && !empty($social_value)) {
			// Settings error
			//@Todo Error message could be nicer
			add_settings_error( 'url_error' . $error_counter, esc_attr( 'invalide_url'. $error_counter ), str_replace('_', '.', __( 'Invalide Url was giving for: ', 'tsv-bierden' ) . ucfirst( $social_key ) ) );
			$valid_proof = false;
			$error_counter++;
		}
	}
	if (!$valid_proof) {
		return $valid_options;
	} else {
		return $input;
	}
}

function bierden_theme_media_options_validate($input) {
	$valid_options = get_option( 'bierden_theme_media_options' );
	$valid_proof = true;
	$error_counter = 0;
	$images_types = array(
		'png' => 'image/png',
		'jpe' => 'image/jpeg',
		'jpeg' => 'image/jpeg',
		'jpg' => 'image/jpeg',
		'gif' => 'image/gif',
		'bmp' => 'image/bmp',
		'ico' => 'image/vnd.microsoft.icon',
		'tiff' => 'image/tiff',
		'tif' => 'image/tiff',
		'svg' => 'image/svg+xml',
		'svgz' => 'image/svg+xml',
	);
	foreach ($input as $social_key => $social_value) {
		$mime_type = wp_check_filetype( $social_value);
		if (!empty($social_value) && !in_array($mime_type['type'], $images_types)) {
			// Settings error
			//@Todo Error message could be nicer
			add_settings_error( 'image_error' . $error_counter, esc_attr( 'invalide_image_url'. $error_counter ), str_replace('_', ' ', __( 'Invalid image type was giving for: ', 'tsv-bierden' ) . ucfirst( $social_key ) ) );
			$valid_proof = false;
			$error_counter++;
		}
	}
	if (!$valid_proof) {
		return $valid_options;
	} else {
		return $input;
	}
}

?>